import os
from pathlib import Path
from typing import Dict, Optional, List
from pydantic_settings import BaseSettings
from pydantic import Field

class Settings(BaseSettings):
    """全局设置对象"""
    
    # 环境变量
    ENV: str = Field(default="dev")
    DEBUG: bool = Field(default=True)
    
    # 项目基本信息
    PROJECT_NAME: str = Field(default="跨境电商竞品分析系统")
    PROJECT_VERSION: str = Field(default="0.1.0")
    API_PREFIX: str = Field(default="/api")
    
    # 路径设置
    BASE_DIR: str = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    STATIC_DIR: str = os.path.join(BASE_DIR, "app", "static")
    UPLOADS_DIR: str = os.path.join(BASE_DIR, "app", "static/uploads")
    PRODUCTS_DIR: str = os.path.join(UPLOADS_DIR, "products")
    
    # 默认图片设置
    DEFAULT_PRODUCT_IMAGE: str = Field(default="Screenshot_20250416_121955.jpg")
    FALLBACK_IMAGE_URL: str = Field(default="https://img.freepik.com/free-vector/image-upload-concept-illustration_114360-798.jpg")
    
    # 文件扩展名设置
    IMAGE_EXTENSIONS: List[str] = Field(default=[".jpg", ".jpeg", ".png", ".gif", ".webp"])
    DOCUMENT_EXTENSIONS: List[str] = Field(default=[".pdf", ".doc", ".docx", ".txt", ".md", ".xls", ".xlsx", ".csv"])
    ALLOWED_FILE_EXTENSIONS: List[str] = Field(default=[".jpg", ".jpeg", ".png", ".gif", ".webp", ".pdf", ".doc", ".docx", ".txt", ".md", ".xls", ".xlsx", ".csv"])
    
    # AI模型设置
    QWEN_API_KEY: str = Field(default="sk-62ac5519d1ad4e36abe7c47c3a43e0bc")
    QWEN_BASE_URL: str = Field(default="https://dashscope.aliyuncs.com/compatible-mode/v1")
    LLM_MODEL_NAME: str = Field(default="qwen-turbo")
    
    # Silicon Flow API设置
    SILICON_FLOW_API_KEY: str = Field(default="sk-xlccqynnzeqmjmspmsjhyvzntlkyyjxfznhoioghzbxqiltz")  # Silicon Flow API密钥
    
    # OCR设置
    BAIDU_APP_ID: str = Field(default="118189211")
    BAIDU_API_KEY: str = Field(default="LSsoTa2wl6dvnlySuMJj6nrH")
    BAIDU_SECRET_KEY: str = Field(default="6UHwsIYGGYqSjWJorvDy0LQT2s6qfZ2N")
    MAX_CONCURRENT_OCR: int = Field(default=2)  # OCR并发数
    
    # 上传文件设置
    ALLOWED_EXTENSIONS: Dict[str, list] = Field(
        default={
            "image": ["jpg", "jpeg", "png", "gif", "webp"],
            "document": ["pdf", "doc", "docx", "txt", "md", "xls", "xlsx", "csv"]
        }
    )
    MAX_CONTENT_LENGTH: int = Field(default=5 * 1024 * 1024)  # 5MB
    
    # 向量数据库配置
    VECTOR_STORE_DIR: str = "vectorstore"
    VECTOR_STORE_TYPE: str = "faiss"  # 可选: "faiss" 或 "chroma"
    
    # LLM API配置
    LLM_API_URL: str = "http://127.0.0.1:8000"  # 默认本地API服务
    
    # 数据目录
    DATA_DIR: str = "data"
    
    class Config:
        env_file = ".env"
        case_sensitive = True

# 创建全局设置实例
settings = Settings()

# 确保必要的文件夹存在
for dir_path in [settings.STATIC_DIR, settings.UPLOADS_DIR, settings.PRODUCTS_DIR]:
    os.makedirs(dir_path, exist_ok=True)

# 应用启动时调用
def ensure_directories():
    """确保应用所需的目录都存在"""
    dirs = [
        settings.STATIC_DIR,
        settings.UPLOADS_DIR,
        settings.PRODUCTS_DIR,
        settings.VECTOR_STORE_DIR,
        settings.DATA_DIR,
        os.path.join(settings.STATIC_DIR, "images"),
        os.path.join(settings.UPLOADS_DIR, "documents"),
    ]
    
    for dir_path in dirs:
        Path(dir_path).mkdir(parents=True, exist_ok=True)

ensure_directories() 